package com.cgx.tablet.beeper.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.cgx.tablet.beeper.bean.User;
import com.cgx.tablet.beeper.utils.security.EncrypAES;

/**
 * SharedPreference工具类
 */
public class SharedPreferenceUtil {

    private static final String SESSION = "session";
    private static final String COOKIE = "cookie";
    private static final String USER = "user";
    private static final String ACCOUNT = "account";
    private static final String PASSWORD = "password";
    private static final String WINDOW = "window";
    private static final String IP = "ip";
    private static final String PORT = "port";

//    /**
//     *对用户名和密码进行加解密
//     */
//    private EncrypAES mAes = new EncrypAES();

    private static SharedPreferences getSharedPreferences(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static void clearData(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        //获取要保留的数据
//        int parBrightness = sp.getInt(PAR_BRIGHTNESS, 127);
//        int shakeBrightness = sp.getInt(SHAKE_BRIGHTNESS, 127);
//        int surfaceBrightness = sp.getInt(SURFACE_BRIGHTNESS, 127);
//        int band24 = sp.getInt(BAND_2_4, -1);
//        String controlBgPath = sp.getString(CONTROL_BG_PATH, "");
        //清除所有数据
        sp.edit().clear().apply();
        //重新设置要保留的数据
//        sp.edit().putBoolean(FIRST_INSTALL, false)
//                .putInt(PAR_BRIGHTNESS, parBrightness)
//                .putInt(SHAKE_BRIGHTNESS, shakeBrightness)
//                .putInt(SURFACE_BRIGHTNESS, surfaceBrightness)
//                .putInt(BAND_2_4, band24)
//                .putString(CONTROL_BG_PATH, controlBgPath)
//                .apply();
    }

    public static void setSession(Context context, String session) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putString(SESSION, session).apply();
    }

    public static String getSession(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(SESSION, "");
    }

    public static void setCookie(Context context, String cookie) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putString(COOKIE, cookie).apply();
    }

    public static String getCookie(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(COOKIE, "");
    }


    public static void setUser(Context context, User user) {
        SharedPreferences sp = getSharedPreferences(context);
        if (user == null) {
            sp.edit().putString(USER, "").apply();
        } else {
            sp.edit().putString(USER, new Gson().toJson(user)).apply();
        }
    }

    public static User getUser(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        String user = sp.getString(USER, "");
        if (TextUtils.isEmpty(user))
            return null;
        return new Gson().fromJson(user, User.class);
    }

    public static void setAccount(Context context, String account) {
        SharedPreferences sp = getSharedPreferences(context);
        try {
            sp.edit().putString(ACCOUNT, new EncrypAES().EncryptorString(account)).apply();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getAccount(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return new EncrypAES().DecryptorString(sp.getString(ACCOUNT, ""));
    }

    public static void setPassword(Context context, String password) {
        SharedPreferences sp = getSharedPreferences(context);
        try {
            sp.edit().putString(PASSWORD, new EncrypAES().EncryptorString(password)).apply();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getPassword(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return new EncrypAES().DecryptorString(sp.getString(PASSWORD, ""));
    }

    public static void setWindow(Context context, int winIndex) {
        SharedPreferences sp = getSharedPreferences(context);
        try {
            sp.edit().putInt(WINDOW, winIndex).apply();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getWindow(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getInt(WINDOW, 0);
    }

    public static void setIP(Context context, String cookie) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putString(IP, cookie).apply();
    }

    public static String getIP(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(IP, "");
    }

    public static void setPort(Context context, String cookie) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putString(PORT, cookie).apply();
    }

    public static String getPort(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(PORT, "");
    }
}
